package com.financing.controller.finance;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresRoles;

import com.financing.common.CONST;
import com.financing.enums.finance.FinanceLogType;
import com.financing.model.Data;
import com.financing.model.finance.FinanceLog;
import com.financing.model.user.User;
import com.financing.service.finance.FinanceLogService;
import com.financing.service.user.UserService;
import com.financing.util.DateUtil;
import com.financing.util.Json;
import com.financing.util.Money;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;

public class FinanceLogController extends Controller {
	private static UserService user = new UserService();
	private static FinanceLogService financeLog = new FinanceLogService();
	
	/**
	 * 财务信息记录列表
	 * @throws Exception 
	 */ 
	@RequiresRoles("admin")  
	public void statistics() throws Exception { 
		
		// 获取数据 2018-01-16 14:00:00
		String createTimeStart = getPara("createTimeStart", DateUtil.getNowDate()+" 00:00:00");
		String createTimeEnd = getPara("createTimeEnd", DateUtil.getNowDate()+" 24:00:00");
		List<FinanceLog> list = FinanceLog.me.find("select * from finance_log where create_time>=? and create_time<=?", createTimeStart, createTimeEnd);
		
		// 平台数据 
		double plat_in_total = 0.00;
		double plat_out_total = 0.00;
		double plat_tyj = 0.00;
		double plat_tzhb = 0.00;
		double plat_xjhb = 0.00;
		double plat_plus = 0.00;
		double plat_jxq = 0.00;
		double plat_benefit = 0.00;
		double plat_full_fee = 0.00;
		double plat_balance = user.findByCode(CONST.PLAT_CODE).getAvailableAmount();
		double plat_deposit = 0.00;

		// 融资机构数据
		double fund_in_total = 0.00;
		double fund_out_total = 0.00;
		double fund_amount = 0.00;
		double fund_interest = 0.00;
		double fund_plat_benefit = 0.00;
		double fund_agent_benefit = 0.00;
		double fund_from_transfer = 0.00;
		double fund_to_transfer = 0.00;
		double fund_balance = User.me.findById(2).getAvailableAmount();
		double fund_deposit = 0.00;
		
		for(FinanceLog fl : list){

			if(1==fl.getFrom()) {
				if(FinanceLogType.TZHB.equals(fl.getType())) {
					plat_tzhb += fl.getAmount();
				}
				if(FinanceLogType.XJHB.equals(fl.getType())) {
					plat_xjhb += fl.getAmount();
				}
				if(FinanceLogType.PLUS.equals(fl.getType())) {
					plat_plus += fl.getAmount();
				}
				if(FinanceLogType.JXQ.equals(fl.getType())) {
					plat_jxq += fl.getAmount();
				}
				if(FinanceLogType.TYJ.equals(fl.getType())) {
					plat_tyj += fl.getAmount();
				}
				plat_out_total+=fl.getAmount();
			}
			if(1==fl.getTo()) {
				if(FinanceLogType.PLAT_BENEFIT.equals(fl.getType())) {
					plat_benefit += fl.getAmount();
				}
				if(FinanceLogType.DEPOSIT.equals(fl.getType())) {
					plat_deposit += fl.getAmount();
				}
				plat_in_total+=fl.getAmount();
			}
			if(2==fl.getFrom()) {
				if(FinanceLogType.AMOUNT.equals(fl.getType())) {
					fund_amount += fl.getAmount();
				}
				if(FinanceLogType.INTEREST.equals(fl.getType())) {
					fund_interest += fl.getAmount();
				}
				if(FinanceLogType.PLAT_BENEFIT.equals(fl.getType())) {
					fund_plat_benefit += fl.getAmount();
				}
				if(FinanceLogType.AGENT_BENEFIT.equals(fl.getType())) {
					fund_agent_benefit += fl.getAmount();
				}
				if(FinanceLogType.TRANSFER.equals(fl.getType())) {
					fund_from_transfer += fl.getAmount();
				}
				fund_out_total+=fl.getAmount();
			}
			if(2==fl.getTo()) {
				if(FinanceLogType.TRANSFER.equals(fl.getType())) {
					fund_to_transfer += fl.getAmount();
				}
				if(FinanceLogType.FULL.equals(fl.getType())) {
					fund_interest += fl.getAmount();
				}
				if(FinanceLogType.DEPOSIT.equals(fl.getType())) {
					fund_deposit += fl.getAmount();
				}
				fund_in_total+=fl.getAmount();
			}
		}
		renderJson(Json.ok("数据获取成功！", 
			Data.put("plat_in_total",Money.format(plat_in_total),
				"plat_out_total",Money.format(plat_out_total),
				"plat_balance",Money.format(plat_balance),
				"plat_deposit",Money.format(plat_deposit),
				"fund_balance",Money.format(fund_balance),
				"fund_deposit",Money.format(fund_deposit),
				"plat_tyj",Money.format(plat_tyj),
				"plat_tzhb",Money.format(plat_tzhb),
				"plat_xjhb",Money.format(plat_xjhb),
				"plat_full_fee",Money.format(plat_full_fee),
				"plat_plus",Money.format(plat_plus),
				"plat_jxq",Money.format(plat_jxq),
				"plat_benefit",Money.format(plat_benefit),
				"fund_in_total",Money.format(fund_in_total),
				"fund_out_total",Money.format(fund_out_total),
				"fund_amount",Money.format(fund_amount),
				"fund_interest",Money.format(fund_interest),
				"fund_plat_benefit",Money.format(fund_plat_benefit),
				"fund_agent_benefit",Money.format(fund_agent_benefit), 
				"fund_from_transfer",Money.format(fund_from_transfer),
				"fund_to_transfer",Money.format(fund_to_transfer)
				)
			)
		);
	}
	
	@RequiresRoles("admin")  
	public void list() throws Exception { 
		int page = getParaToInt("page",1);
		int size = getParaToInt("size",10);
		Map<String, String> params = Data.flat(getParaMap());
		Page<FinanceLog> p = financeLog.list(page,size,params);
		for(FinanceLog fl : p.getList()) {
			if(fl.getFrom()>0) {
				User from = User.me.findById(fl.getFrom());
				if(from!=null) {
					fl.put("fromRealName", from.getRealName());
				}
			}
		}
		renderJson(Json.ok("数据获取成功！", Data.put("page",p)));
	}
}
 